package mini.ideashare.cms.controller;

import com.taobao.api.ApiException;
import com.taobao.api.TaobaoClient;
import com.taobao.api.domain.TbkFavorites;
import com.taobao.api.domain.UatmTbkItem;
import com.taobao.api.request.TbkUatmFavoritesGetRequest;
import com.taobao.api.request.TbkUatmFavoritesItemGetRequest;
import com.taobao.api.response.TbkUatmFavoritesGetResponse;
import com.taobao.api.response.TbkUatmFavoritesItemGetResponse;
import mini.ideashare.cms.base.BaseResponse;
import mini.ideashare.cms.base.PageData;
import mini.ideashare.cms.common.TBClientFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class FavoriteController extends AbstractBaseController {

    @GetMapping("/favorite/listFavorites")
    @ResponseBody
    public BaseResponse<PageData<TbkFavorites>> listFavorites(TbkUatmFavoritesGetRequest req){
        TaobaoClient client = TBClientFactory.getClient();
        TbkUatmFavoritesGetResponse rsp = null;
        try {
            req.setFields("favorites_title,favorites_id,type");
            rsp = client.execute(req);
        } catch (ApiException e) {
            e.printStackTrace();
        }
        int pageNo = req.getPageNo()==null?0:req.getPageNo().intValue();
        int pageSize = req.getPageSize()==null?0:req.getPageSize().intValue();

        return  BaseResponse.assemblePageResponse(rsp.getResults(),rsp.getTotalResults(),pageNo,pageSize);

    }

    @GetMapping("/favorite/listFavoriteItem")
    @ResponseBody
    public BaseResponse<PageData<UatmTbkItem>> getFavorite(TbkUatmFavoritesItemGetRequest req){
        TaobaoClient client = TBClientFactory.getClient();
        req.setFields("num_iid,title,pict_url,small_images,reserve_price,zk_final_price,user_type,provcity,item_url,seller_id," +
                "volume,nick,shop_title,zk_final_price_wap,event_start_time,event_end_time,tk_rate,status," +
                "type,category,click_url,commission_rate,coupon_click_url,coupon_info,coupon_start_time,coupon_end_time,coupon_remain_count,coupon_total_count");
        req.setAdzoneId(51957800188l);
        TbkUatmFavoritesItemGetResponse rsp = null;
        try {
            rsp = client.execute(req);
        } catch (ApiException e) {
            e.printStackTrace();
        }
        int pageNo = req.getPageNo()==null?0:req.getPageNo().intValue();
        int pageSize = req.getPageSize()==null?0:req.getPageSize().intValue();

        return  BaseResponse.assemblePageResponse(rsp.getResults(),rsp.getTotalResults(),pageNo,pageSize);

    }
}
